package com.jyt.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyt.entities.Check;
import com.jyt.mapper.CheckMapper;
import com.jyt.service.CheckService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:  查询检测表
 * @Author hugh.sun
 * @Date 2021/11/24 16:48
 */
@Slf4j
@Service
public class CheckServiceImpl implements CheckService {
    @Resource
    private CheckMapper checkMapper;

    @Override
    @Transactional
    public PageInfo<Check> findAll(Integer pageNum,
                                   Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Check> allCheck = checkMapper.findAll();

        PageInfo pageInfo = new PageInfo(allCheck);

        return pageInfo;
    }

    @Override
    @Transactional
    public PageInfo<Check> findAllCheck( Integer pageNum,
                                         Integer pageSize,Check check) {

        PageHelper.startPage(pageNum, pageSize);
        List<Check> allCheck = checkMapper.findAllCheck(check);
        PageInfo pageInfo = new PageInfo(allCheck);

        return pageInfo;
    }

    @Override
    @Transactional
    public void addCheck(Check check) {
        checkMapper.addCheck(check);
    }

    @Override
    @Transactional
    public String excel(){
        try {
            List<Check> all = checkMapper.findAll();
            EasyExcel.write("E:\\检测报告.xls")

                    .head(Check.class)
                    .excelType(ExcelTypeEnum.XLS)
//                    自适应宽度但是不是特别准确
                   /* .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())*/
                    .sheet("检测报告")
                    .doWrite(all);
            return "all";
        } catch (Exception e) {
            e.printStackTrace();
            return "excel报错";
        }
    }


}
